package com.xiguaji.tiktok.task;

import com.xiguaji.boot.retry.RetryableException;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import com.xxl.job.core.log.XxlJobLogger;
import org.junit.jupiter.api.Test;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

@Component
public class RetryableService {

    @Retryable(RetryableException.class)
    @Test
    public void aaa() {
        System.out.println("111");
        XxlJobLogger.log("test{}",123);
        // throw new RetryableException("xxx");
    }
    @Test
    public void syn() throws ExecutionException, InterruptedException {
        List<Integer> rst = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            rst.add(i);
        }
        System.out.println("cpu:"+Runtime.getRuntime().availableProcessors());

        ForkJoinPool forkJoinPool = new ForkJoinPool(15);
        PollInfo pollInfo=new PollInfo();
        new Thread(()->

                {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (!forkJoinPool.isShutdown()) {
                        pollInfo.setPoolSize(forkJoinPool.getPoolSize());
                        pollInfo.setActiveThreadCount(forkJoinPool.getActiveThreadCount());
                        pollInfo.setRunningThreadCount(forkJoinPool.getRunningThreadCount());
                        pollInfo.setStealCount(forkJoinPool.getStealCount());
                        pollInfo.setQueuedSubmissionCount(forkJoinPool.getQueuedSubmissionCount());
                        pollInfo.setQueuedTaskCount( forkJoinPool.getQueuedTaskCount());
                        pollInfo.setParallelism(forkJoinPool.getParallelism());
                        try {
                            Thread.sleep(1000);
                            System.out.println(pollInfo);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            ).start();
        forkJoinPool.submit( () ->{       rst.parallelStream().forEach(i -> {
            System.out.println(Thread.currentThread().getName() + ":" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });}).get();

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }
    static class PollInfo{
        private int poolSize;
        private int activeThreadCount;
        private int queuedSubmissionCount;
        private long stealCount;
        private int runningThreadCount;
        private long queuedTaskCount;
        private int parallelism;

        @Override
        public String toString() {
            return "PollInfo{" +
                    "poolSize=" + poolSize +
                    ", activeThreadCount=" + activeThreadCount +
                    ", queuedSubmissionCount=" + queuedSubmissionCount +
                    ", stealCount=" + stealCount +
                    ", runningThreadCount=" + runningThreadCount +
                    ", queuedTaskCount=" + queuedTaskCount +
                    ", parallelism=" + parallelism +
                    '}';
        }

        public long getQueuedTaskCount() {
            return queuedTaskCount;
        }

        public int getParallelism() {
            return parallelism;
        }

        public void setParallelism(int parallelism) {
            this.parallelism = parallelism;
        }

        public void setQueuedTaskCount(long queuedTaskCount) {
            this.queuedTaskCount = queuedTaskCount;
        }

        public int getPoolSize() {
            return poolSize;
        }

        public void setPoolSize(int poolSize) {
            this.poolSize = poolSize;
        }

        public int getActiveThreadCount() {
            return activeThreadCount;
        }

        public void setActiveThreadCount(int activeThreadCount) {
            this.activeThreadCount = activeThreadCount;
        }

        public int getQueuedSubmissionCount() {
            return queuedSubmissionCount;
        }

        public void setQueuedSubmissionCount(int queuedSubmissionCount) {
            this.queuedSubmissionCount = queuedSubmissionCount;
        }

        public long getStealCount() {
            return stealCount;
        }

        public void setStealCount(long stealCount) {
            this.stealCount = stealCount;
        }

        public int getRunningThreadCount() {
            return runningThreadCount;
        }

        public void setRunningThreadCount(int runningThreadCount) {
            this.runningThreadCount = runningThreadCount;
        }
    }
}
